<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller;


use app\admin\service\NodeService;
use app\common\facade\Arr;
use library\Controller;
use library\tools\Data;
use think\Console;
use think\Db;
use think\exception\HttpResponseException;
use app\common\facade\TransItem;
use app\common\services\JobService;
use app\common\services\PublishJobService;
use app\common\tools\ExportExcel;

/**
 * 兼职管理
 * Class Index
 * @package app\admin\controller
 */
class Job extends Controller
{
    public $table = 'Jobs';

    /**
     * 兼职概况
     * @auth true
     * @menu true
     */
    public function index()
    {
        $result['total'] = Db::name('Jobs')->where('status!=0')->count();//全部兼职
        $result['beaudite']=Db::name('Jobs')->where('status=1')->count();//待审核
        $result['pass']=Db::name('Jobs')->where('status=2')->count();//审核通过兼职
        $result['deny']=Db::name('Jobs')->where('status=3')->count();//审核拒绝
        $result['work']=Db::name('Jobs')->where('status=4')->count();//工作中兼职
        $result['complete']=Db::name('Jobs')->where('status=5')->count();//已完成兼职
        $this->assign('result',$result);

        /*$jobtype=DB::name("JobsType")->field("name,job_num")->select();
        $this->assign('jobtype',$jobtype);*/
        $this->fetch();
    }

    /**
    * 兼职列表
    * @auth true
    * @menu true
    */
    public function job_list(){

        $this->title = '兼职列表';
        $work_type=DB::name("JobsType")->field("id,name")->select();
        $this->assign('work_type',$work_type);

        $status = $this->request->param('status', 0);
        if($status!=0){
            $dat[]=['status','EQ',$status];
            if($status==1){
                $this->title = '待审核兼职列表';
            }elseif ($status==2) {
                $this->title = '审核通过兼职列表';
            }elseif ($status==3) {
                $this->title = '审核拒绝兼职列表';
            }elseif ($status==4) {
               $this->title = '工作中兼职列表';
            }else{
                $this->title = '已完成兼职列表';
            }
        }else{
            $dat[]=['status','NEQ',$status];
        }
        $query = $this->_query($this->table)
                ->where($dat)
                ->like('title')
                ->equal('id,mobile,work_type,is_top,contacts');
        $query->order('id desc')->page();



    }
     /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _job_list_page_filter(&$data)
    {

        $this->clist = Db::name('JobsType')->field("id,name")->select();
        foreach ($data as &$vo) {
            $vo['salary_unitCh'] = TransItem::transMoneyUnit(Arr::get($vo, 'salary_unit'));
            $vo['province'] = DB::name("Area")->where('area_id',$vo['provinces_id'])->value("area_name");
            $vo['city'] = DB::name("Area")->where('area_id',$vo['city_id'])->value("area_name");
            $vo['settlement_typeCh'] = TransItem::transSettlement(Arr::get($vo, 'settlement_type'));
            $vo['add_time'] =date("Y-m-d",$vo['created_at']);
            foreach ($this->clist as $cate) if ($cate['id'] === $vo['work_type']) $vo['cate'] = $cate;

        }
    }

    
    /**
    * 兼职详情
    * @auth true
    */
    public function editJob()
    {   

        $jobId = $this->request->param('id', 0);
        $extend = "content,userApply";
        $extend = array_filter(explode(',', $extend));
        $result=(new JobService())->jobDetail($jobId,$extend);
        $result['work_time']=date("Y-m-d",$result['work_time']);
        $result['work_time_end']=date("Y-m-d",$result['work_time_end']);
        $result['created_at']=date("Y-m-d H:i:s",$result['created_at']);
        $this->assign('result',$result);

        //兼职类型
        $jobtype=Db::name('JobsType')
            ->field("id,name")
            ->where('status=1')->select();
        $this->assign('jobtype',$jobtype);

        //薪资单位
        $salary_unit= TransItem::transMoneyUnit();
        $this->assign('salary_unit',$salary_unit);

        //工作状态
        $jobstatus= TransItem::transJobApply();
        $this->assign('jobstatus',$jobstatus);

         //结算方式
        $settlement= TransItem::transSettlement();
        $this->assign('settlement',$settlement);


        $this->title ='编辑兼职|兼职编号：'.$result['id'];
        if($result['admin_id']==0){
            $user=DB::name("User")->where('id',$result['user_id'])->field("truename,mobile")->find();
            $this->title ='编辑兼职|兼职编号：'.$result['id']."|注册人：".$user['truename']."|联系电话：".$user['mobile'];
        }
        $this->fetch('edit_job');
              
       
    }
   
    /**
    * 报名列表
    * @auth true
    * 
    */
    public function apply(){

        $jobId = $this->request->param('id', 0);
        $query = $this->_query("JobsApply")
            ->where('job_id',$jobId)
            ->like('title')
            ->equal('status');
        $query->order('id desc')->page();
    }
    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _apply_page_filter(&$data)
    {

        foreach ($data as &$vo) {
            $vo['user'] = DB::name("UserExtend")->where('user_id',$vo['user_id'])->field("true_name,mobile")->find(); 
            $vo['add_time']=date("Y-m-d H:i",$vo['created_at']);
            $vo['jstatus'] = TransItem::transJobApply(Arr::get($vo, 'status'));
        }
      

    }

    /**
    * 导出报名表
    * @auth true
    * 
    */
    public function export()
    {

        $jobid = $this->request->param('id', 0);
        $job=DB::name("Jobs")->where('id',$jobid)->field("title,work_time")->find();
        $title="兼职标题|".$job['title']."|兼职时间|".date("Y-m-d",$job['work_time']);
        $data = Db::name('JobsApply')->alias('j')
            ->where('j.job_id', $jobid)
            ->join('jobs js', 'js.id = j.job_id')
            ->join('UserExtend ue', 'j.user_id = ue.user_id')
            ->field('js.title as jobName,ue.true_name as user_true_name,ue.mobile as user_mobile,j.status,j.created_at')
            ->select();
        foreach ($data as $key => $value) {
           $result[$key]['jobName']=$value['jobName'];
           $result[$key]['true_name']=$value['user_true_name'];
           $result[$key]['mobile']=$value['user_mobile'];
           $result[$key]['jstatus'] = TransItem::transJobApply(Arr::get($value, 'status'));
           $result[$key]['add_time']=date("Y-m-d H:i",$value['created_at']);
        }

        $exportdata = [
            'cellTitle'     => [
                [
                    'name'      => '兼职标题',
                    'dataName'  => 'jobName',
                ],
                [
                    'name'      => '姓名',
                    'dataName'  => 'true_name',
                ],
                [
                    'name'      => '联系电话',
                    'dataName'  => 'mobile',
                ],
                [
                    'name'      => '状态',
                    'dataName'  => 'jstatus',
                ],
                [
                    'name'      => '报名时间',
                    'dataName'  => 'add_time',
                ]
            ],
            'list'          => $result,
            'otherExportList'     => [
                [
                    'sheetIndex'    => 1,
                    'sheetTitle'    => "$title",
                    'list'  => $result,
                    'cellTitle'     => [
                        [
                            'name'      => '兼职标题',
                            'dataName'  => 'jobName',
                        ],
                        [
                            'name'      => '姓名',
                            'dataName'  => 'true_name',
                        ],
                        [
                            'name'      => '联系电话',
                            'dataName'  => 'mobile',
                        ],
                        [
                            'name'      => '状态',
                            'dataName'  => 'jstatus',
                        ],
                        [
                            'name'      => '报名时间',
                            'dataName'  => 'add_time',
                        ]
                    ],
                ]
            ]
        ];
        (new ExportExcel())->exportDataToXls(
            "$title",
            $exportdata
        );
    }




}
